package com.dragon.app.controller.auth;


import com.dragon.app.config.annotation.Log;
import com.dragon.common.model.Insert;
import com.dragon.common.model.ResponseData;
import com.dragon.common.model.Update;
import com.dragon.logical.service.auth.DepartmentService;
import com.dragon.repository.domain.dto.DepartmentDTO;
import com.dragon.repository.domain.vo.DepartmentVO;
import com.dragon.repository.entity.Department;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门 后台 接口
 * @author Charlotte
 */
@RestController
@Api(tags = "部门管理")
@RequestMapping("department")
public class DepartmentController {

    @Autowired
    DepartmentService service;

    @GetMapping("list")
    @ApiOperation(value = "部门列表")
    public ResponseData<List<DepartmentDTO>> selectDepartmentList(@ApiParam(name = "search", value = "搜索参数",required = false,type = "spring") @RequestParam(required = false) String search){
        List<Department> departments = service.selectDepartmentList(search);
        return ResponseData.success(DepartmentDTO.convertList(departments));
    }

    @PostMapping("add")
    @ApiOperation(value = "添加部门")
    @Log
    public ResponseData addDepartment(@RequestBody @Validated(Insert.class) DepartmentVO vo) {
        service.addDepartment(DepartmentVO.convert(vo));
        return ResponseData.success();
    }

    @PutMapping("update")
    @ApiOperation(value = "更新部门")
    @Log
    public ResponseData updateDepartment(@RequestBody @Validated(Update.class) DepartmentVO vo){
        service.updateDepartment(DepartmentVO.convert(vo));
        return ResponseData.success();
    }

    @DeleteMapping("delete")
    @ApiOperation("删除部门")
    @Log
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", required = true, type = "int", value = "部门id")
    })
    public ResponseData deleteDepartment(@RequestParam Integer id){
        service.deleteDepartment(id);
        return ResponseData.success();
    }

    @PostMapping("deleteList")
    @ApiOperation("批量删除部门")
    @Log
    public ResponseData deleteDepartmentList( @ApiParam(name = "ids", type = "array", required = true, value = "部门id集合") @RequestBody List<Integer> ids) {
        service.deleteDepartmentList(ids);
        return ResponseData.success();
    }
}
